package com.arvin.controller;

import com.arvin.pojo.Interview;
import com.arvin.pojo.Log;
import com.arvin.pojo.PageBean;
import com.arvin.pojo.Result;
import com.arvin.service.InterviewService;
import com.arvin.service.LogService;
import com.arvin.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin")
@Tag(name = "面试相关接口",description = "面试相关接口")
@Slf4j
public class InterviewController {

    @Autowired
    private InterviewService interviewService;
    @Autowired
    private LogService logService;

    /**
     * 获取面试信息
     * @param page
     * @param pageSize
     * @param studentId
     * @param intvDate
     * @param stuName
     * @param className
     * @param intvState
     * @param intvScore
     * @return
     */
    @Operation(summary = "获取面试信息",description = "获取面试信息")
    @GetMapping("/intv/getIntvInfo")
    public Result<PageBean> getIntvInfo(Integer page, Integer pageSize, String studentId,
                                        LocalDate intvDate, String stuName,String className,
                                        String intvState,Integer intvScore){
        // 默认面试时间排序asc升序、查询,分页
        PageBean pageBean = interviewService.getIntvInfo(page, pageSize, studentId,
                intvDate, stuName,className, intvState,intvScore);
        log.info("面试信息{}", pageBean);
        return Result.success(pageBean);
    }

    /**
     * 学生面试成绩打分
     * @param interview
     * @return
     */
    @Operation(summary = "学生面试成绩打分",description = "学生面试成绩打分")
    @PutMapping("/intv/setIntvScore")
    public Result setIntvScore(@RequestBody Interview interview) {
        Integer intvId = interview.getIntvId();
        Integer intvScore = interview.getIntvScore();
        String intvState = interview.getIntvState();
        interviewService.updateIntvState(intvId, intvState,intvScore);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("考试");
        log.setLogOperate("管理员" + username + "为学号为" + interview.getStudentId()
                + "的学生进行面试打分:" + intvScore + "并修改面试状态为：" + intvState);
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 学生批量通过面试
     * @param ids
     * @return
     */
    @Operation(summary = "学生批量通过面试")
    @PutMapping("/intv/passByIntvIds/{ids}")
    public Result passByIntvIds(@PathVariable List<Integer> ids) {
        interviewService.passByIntvIds(ids,"已通过");

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("面试");
        log.setLogOperate("管理员" + username + "将面试id为" + ids
                + "的面试状态修改为：" + "已通过");
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 学生批量不通过面试
     * @param ids
     * @return
     */
    @Operation(summary = "学生批量不通过面试")
    @PutMapping("/intv/noPassByIntvIds/{ids}")
    public Result noPassByIntvIds(@PathVariable List<Integer> ids) {
        interviewService.passByIntvIds(ids,"未通过");

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("面试");
        log.setLogOperate("管理员" + username + "将面试id为" + ids
                + "的面试状态修改为：" + "未通过");
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }
}
